const ProxyPool = require('../index.js');
const requestAsync = require("../lib/utils/http_util.js");
const ProxyConsts = require('../lib/proxy_consts.js');

let pool = new ProxyPool();
pool.on("has_proxy", (proxy) => {
  test_proxy(proxy);
});
pool.start();

async function test_proxy(proxy) {
  let url;
  if(proxy.protocol == 'https') {
    url = 'https://www.baidu.com/img/bd_logo1.png';
  } else if(proxy.protocol == 'http') {
    url = "http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/prettify.css";
  }
  let all = pool.list();
  console.log("proxy all:", all);

  try {
    let timeout = 20000;
    let reqeustOpts = {
      url: url,
      timeout: timeout,
      proxy: pool.proxyUrl(proxy),
      headers: ProxyConsts.HTTP_HEADERS
    };
    let startTime = +new Date;
    let body = await requestAsync(reqeustOpts);
    console.log("测试正常!");
  } catch(error) {
    console.error("请求url报错:", error);
  }
}
setInterval(() => {}, 1000);